package com.cn.longdistancebusstation.adapter;

import android.content.Context;
import android.icu.text.SimpleDateFormat;
import android.os.Build;
import android.support.annotation.RequiresApi;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.cn.longdistancebusstation.R;
import com.cn.longdistancebusstation.model.Coupons;

import java.math.BigDecimal;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created by mucheng on 2018/2/27.
 */

public class CouponAdapter2 extends BaseAdapter{
    private Context mContext;
    private List<Coupons> mCouponsList;
    LayoutInflater mLayoutInflater;
    private BigDecimal mBigDecimal;
    public void setCouponsList(List<Coupons> mCouponsList) {
        mCouponsList = mCouponsList;

    }
    public CouponAdapter2(Context mContext,List<Coupons> mCouponsList){
        this.mContext=mContext;
        mLayoutInflater = LayoutInflater.from(mContext);
        if(mCouponsList!=null){
            this.mCouponsList=mCouponsList;
        }else{
            this.mCouponsList=new ArrayList<>();
        }

    }
    @Override
    public int getCount() {
        return mCouponsList.size();
    }

    @Override
    public Object getItem(int i) {
        return mCouponsList.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @RequiresApi(api = Build.VERSION_CODES.N)
    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        ViewHolder holder=null;
        if(view==null){
            holder=new ViewHolder();
            view= mLayoutInflater.inflate(R.layout.view_coupons,null);
            holder.mTicketCouponTextView2=(TextView)view.findViewById(R.id.history_coupon_discount);
            holder.mRangeOfApplicationIllustrateTextView2 = (TextView)view.findViewById(R.id.history_range_of_application_illustrate);
            holder.mPeriodOfValidityTextView2=(TextView)view.findViewById(R.id.history_period_of_validity_illustrate);
            holder.mLoseEfficacyRelativeLayout=(RelativeLayout)view.findViewById(R.id.info_of_coupons);
            holder.mUsedRelativeLayout=(RelativeLayout)view.findViewById(R.id.info_of_coupons2);
            view.setTag(holder);
        }else{
            holder=(ViewHolder)view.getTag();
        }
        Coupons coupons=mCouponsList.get(i);
        holder.mTicketCouponTextView2.setText(coupons.getCouponX());
        SimpleDateFormat formatter = new SimpleDateFormat ("yyyy-MM-dd");
        String currentDate=formatter.format(new java.util.Date());
        String strPeriodOfValidity=coupons.getEffectiveTime();
        String strTime=coupons.getStartDate();
        Date time1=null;
        Date currentTime=null;
        int time2=Integer.parseInt(strPeriodOfValidity);
        try {
            time1 = formatter.parse(strTime);
            currentTime=formatter.parse(currentDate);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        Date newDate2 = new Date(time1.getTime() + time2 * 24 * 60 * 60 * 1000);
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        String dateOk = simpleDateFormat.format(newDate2);
        Log.d("时间",dateOk);
        holder.mRangeOfApplicationIllustrateTextView2.setText(dateOk);
        if(time1.getTime()>currentTime.getTime()){
            holder.mLoseEfficacyRelativeLayout.setVisibility(View.VISIBLE);
            holder.mUsedRelativeLayout.setVisibility(View.GONE);
        }else{
            holder.mUsedRelativeLayout.setVisibility(View.VISIBLE);
            holder.mLoseEfficacyRelativeLayout.setVisibility(View.GONE);
        }
        return view;
    }
    class ViewHolder{
        TextView mTicketCouponTextView2;
        TextView mRangeOfApplicationIllustrateTextView2;
        TextView mPeriodOfValidityTextView2;
        RelativeLayout mUsedRelativeLayout;
        RelativeLayout mLoseEfficacyRelativeLayout;


    }
}
